package com.compayname.projectname.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.compayname.projectname.base.data.ResJsonData;
import com.compayname.projectname.controller.data.DeviceData;
import com.compayname.projectname.controller.data.InstallaMaintenData;
import com.compayname.projectname.data.DeviceReq;
import com.compayname.projectname.data.DghyCrmorderInfoVo;
import com.compayname.projectname.data.InstallmaintainReq;
import com.compayname.projectname.service.InterService;
import org.junit.platform.commons.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/work")
public class WorkController {

    private static Logger logger = LoggerFactory.getLogger(WorkController.class);

    @Autowired
    private InterService interService;

    /**
     * 页面返回
     */
    @RequestMapping(value="/index")
    public String index(Model model){
        return "deviceIndex";
    }

    @RequestMapping(value="/index2")
    @ResponseBody
    public ModelAndView goHome(){
        ModelAndView mode = new ModelAndView();
        mode.setViewName("indexMGR");
        return mode;
    }

    @RequestMapping(value = "/queryList/{page}/{pageSize}")
    @ResponseBody
    public Object queryList(HttpServletRequest request, HttpServletResponse response, @PathVariable("page") Long page, @PathVariable("pageSize") Long pageSize) {
        logger.debug("=====invoke queryList start...");
        long start = System.currentTimeMillis();
        ResJsonData resJson = new ResJsonData();
        List<InstallaMaintenData> list = new ArrayList<InstallaMaintenData>();

        //获取token
        String token = interService.getToken("MTU5MDAwMDAwMDhbI10xMjEyMTJhQiE");
        //调用数据接口
        InstallmaintainReq req = new InstallmaintainReq();
        DghyCrmorderInfoVo dghyCrmorderInfoVo = new DghyCrmorderInfoVo();
        dghyCrmorderInfoVo.setStatusCd(0);
        req.setDghyCrmorderInfoVo(dghyCrmorderInfoVo);
        req.setSize(pageSize);
        req.setCurrent(page);
        String result = interService.getInstallmaintainList(req, token);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            String iResult = jsonObject.getString("code");
            if ("1".equals(iResult)) {
                String resultStr = jsonObject.getString("result");
                JSONObject dataObject = JSONObject.parseObject(resultStr);

                Integer total = dataObject.getInteger("total");
                JSONArray jsonArray = dataObject.getJSONArray("records");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject vo = jsonArray.getJSONObject(i);
                    InstallaMaintenData installInfo = new InstallaMaintenData();
                    installInfo.setAccessNum(vo.getString("accessNum"));
                    installInfo.setCustName(vo.getString("custName"));
                    installInfo.setCustAddr(vo.getString("custAddr"));
                    installInfo.setCustTel(vo.getString("custTel"));
                    installInfo.setCityName(vo.getString("cityName"));
                    installInfo.setStatusCd(vo.getInteger("statusCd"));
                    installInfo.setEqptTypeName(vo.getString("eqptTypeName"));
                    list.add(installInfo);
                }
                resJson.setData(list);
                resJson.setTotal(total);
            }
        }

//        List<InstallaMaintenData> list = new ArrayList<InstallaMaintenData>();
//        if (page == 1) {
//            InstallaMaintenData data1 = new InstallaMaintenData();
//            data1.setAccessNum("20200825004");
//            data1.setClientName("user1");
//            data1.setClientAddress("gz");
//            list.add(data1);
//            InstallaMaintenData data2 = new InstallaMaintenData();
//            data2.setAccessNum("222");
//            data2.setClientName("user2");
//            data2.setClientAddress("jx");
//            list.add(data2);
//            InstallaMaintenData data3 = new InstallaMaintenData();
//            data3.setAccessNum("333");
//            data3.setClientName("user3");
//            data3.setClientAddress("ab");
//            list.add(data3);
//            InstallaMaintenData data4 = new InstallaMaintenData();
//            data4.setAccessNum("444");
//            data4.setClientName("user4");
//            data4.setClientAddress("qw");
//            list.add(data4);
//            InstallaMaintenData data5 = new InstallaMaintenData();
//            data5.setAccessNum("555");
//            data5.setClientName("user5");
//            data5.setClientAddress("yu");
//            list.add(data5);
//        }
//        if (page == 2) {
//            InstallaMaintenData data6 = new InstallaMaintenData();
//            data6.setAccessNum("66668888");
//            data6.setClientName("user6");
//            data6.setClientAddress("gz");
//            list.add(data6);
//            InstallaMaintenData data7 = new InstallaMaintenData();
//            data7.setAccessNum("777");
//            data7.setClientName("user7");
//            data7.setClientAddress("jx");
//            list.add(data7);
//            InstallaMaintenData data8 = new InstallaMaintenData();
//            data8.setAccessNum("888");
//            data8.setClientName("user8");
//            data8.setClientAddress("ab");
//            list.add(data8);
//        }
//        resJson.setData(list);

        logger.debug("=====invoke queryList end！耗时:{}...", (System.currentTimeMillis() - start));
        logger.debug("=====table data:ret:{}", JSON.toJSONString(resJson));
        return resJson;
    }

    @RequestMapping(value="/toDeviceDetail", method = RequestMethod.GET)
    public String toDeviceDetail(HttpServletRequest request, Model model){
        String custName = request.getParameter("custName");
        String accessNum = request.getParameter("accessNum");
        String eqptTypeName = request.getParameter("eqptTypeName");
        model.addAttribute("custName", custName);
        model.addAttribute("accessNum", accessNum);
        model.addAttribute("eqptTypeName", eqptTypeName);
        //获取token
        String token = interService.getToken("MTU5MDAwMDAwMDhbI10xMjEyMTJhQiE");
        //调用数据接口
        DeviceReq req = new DeviceReq();
        req.setAccessNum(accessNum);
        String result = interService.getDeviceInfo(req, token);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            String iResult = jsonObject.getString("code");
            if ("1".equals(iResult)) {
                String resultStr = jsonObject.getString("result");
                JSONObject dataObject = JSONObject.parseObject(resultStr);
                model.addAttribute("eqptNum", dataObject.getString("eqptNum"));
                model.addAttribute("onlineEqptNum", dataObject.getString("onlineEqptNum"));
                model.addAttribute("offlineEqptNum", dataObject.getString("offlineEqptNum"));
                model.addAttribute("cameraNum", dataObject.getString("cameraNum"));
                model.addAttribute("onlineCameraNum", dataObject.getString("onlineCameraNum"));
                model.addAttribute("offlineCameraNum", dataObject.getString("offlineCameraNum"));
            }
        }
        return "deviceDetail";
    }

    @RequestMapping(value = "/queryDeviceList/{accessNum}/{page}/{pageSize}")
    @ResponseBody
    public Object queryDeviceList( @PathVariable("accessNum") String accessNum, @PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize,
                                   Model model, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("=====invoke queryDeviceList start...");
        logger.debug("=====accessNum:" + accessNum);
        long start = System.currentTimeMillis();
        ResJsonData resJson = new ResJsonData();
        List<DeviceData> list = new ArrayList<DeviceData>();

        //获取token
        String token = interService.getToken("MTU5MDAwMDAwMDhbI10xMjEyMTJhQiE");
        //调用数据接口
        DeviceReq req = new DeviceReq();
        req.setAccessNum(accessNum);
        String result = interService.getCameraList(req, token);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            String iResult = jsonObject.getString("code");
            if ("1".equals(iResult)) {
                JSONArray jsonArray = jsonObject.getJSONArray("result");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject vo = jsonArray.getJSONObject(i);
                    DeviceData deviceInfo = new DeviceData();
                    deviceInfo.setEqptAcct(vo.getString("eqptAcct"));
                    deviceInfo.setCameraNum(vo.getString("cameraNum"));
                    deviceInfo.setOnlineCameraNum(vo.getString("onlineCameraNum"));
                    deviceInfo.setOnlineDesc(vo.getString("onlineDesc"));
                    deviceInfo.setRegTime(vo.getString("regTime"));
                    list.add(deviceInfo);
                }
                resJson.setData(list);
//                resJson.setTotal(total);
            }
        }

//        List<DeviceData> list = new ArrayList<DeviceData>();
//        DeviceData data1 = new DeviceData();
//        data1.setDeviceId(123);
//        data1.setChannelNum("0");
//        data1.setStatus("离线");
//        list.add(data1);
//        DeviceData data2 = new DeviceData();
//        data2.setDeviceId(222);
//        data2.setChannelNum("1");
//        data2.setStatus("在线");
//        list.add(data2);
//        resJson.setData(list);

        logger.debug("=====invoke queryDeviceList end！耗时:{}...", (System.currentTimeMillis() - start));
        logger.debug("=====table data:ret:{}", JSON.toJSONString(resJson));
        return resJson;
    }

    @RequestMapping(value="/toConfigDetail", method = RequestMethod.GET)
    public String toConfigDetail(HttpServletRequest request, Model model){
        String eqptAcct = request.getParameter("eqptAcct");
        logger.debug("====eqptAcct:" + eqptAcct);
        //获取token
        String token = interService.getToken("MTU5MDAwMDAwMDhbI10xMjEyMTJhQiE");
        //摄像头信息
        DeviceReq req1 = new DeviceReq();
//        req1.setId();
        String result1 = interService.getCameraInfo(req1, token);
        if (StringUtils.isNotBlank(result1)) {
            JSONObject jsonObject = JSONObject.parseObject(result1);
            String iResult = jsonObject.getString("code");
            if ("1".equals(iResult)) {
                String resultStr = jsonObject.getString("result");
                JSONObject dataObject = JSONObject.parseObject(resultStr);
                model.addAttribute("chnlName", dataObject.getString("chnlName"));
                model.addAttribute("serialNo", dataObject.getString("serialNo"));
                model.addAttribute("assetNumber", dataObject.getString("assetNumber"));
                model.addAttribute("chnlType", dataObject.getString("cameraNum"));
                model.addAttribute("companyAgreement", dataObject.getString("companyAgreement"));
                model.addAttribute("installationSite", dataObject.getString("installationSite"));
            }
        }
        // 获取sip相关配置信息
        DeviceReq req2 = new DeviceReq();
//        req2.setSipArea();
        String result2 = interService.getSIPServiceInfo(req2, token);
        if (StringUtils.isNotBlank(result1)) {
            JSONObject jsonObject = JSONObject.parseObject(result2);
            String iResult = jsonObject.getString("code");
            if ("1".equals(iResult)) {
                String resultStr = jsonObject.getString("result");
                JSONObject dataObject = JSONObject.parseObject(resultStr);
                model.addAttribute("servId", dataObject.getString("servId"));
                model.addAttribute("chnlAcct", dataObject.getString("chnlAcct"));
                model.addAttribute("sipArea", dataObject.getString("sipArea"));
                model.addAttribute("servIp", dataObject.getString("servIp"));
                model.addAttribute("servNatIp", dataObject.getString("servNatIp"));
                model.addAttribute("sipPort", dataObject.getInteger("sipPort"));
                model.addAttribute("pCode", "123456");
            }
        }
        return "configDetail";
    }

//    @RequestMapping(value="/weuiIndex")
//    @ResponseBody
//    public ModelAndView weuiIndex(){
//        ModelAndView mode = new ModelAndView();
//        mode.setViewName("weuiIndex");
//        return mode;
//    }

}
